import configdata from './config'
import api from './httpLbApi.js'
var version = '1.0'
module.exports = {

	post: function(url, data) {
		url = configdata.APIHOST + url;
		switch (uni.getSystemInfoSync().platform){
			case 'android':
				url += '?medium=android' + '&version=' + version;
				break;
		
			case 'ios':
				url += '?medium=ios' + '&version=' + version;
				break;
		
			default:
		
				url += '?medium=mini' + '&version=' + version;
				break;
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: url,
				data: data,
				method: 'POST',
				header: {
					"Authorization" : 'Bearer ' + uni.getStorageSync('USERINFO'),
					'timestamp': new Date().getTime(),
					
				},
				
				success: function(res) {
					if (res.data.code == 0 || res.data.code == 3) {
						resolve(res.data);
					} else if (res.data.code == 2) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
						 clearTimeout();
						return false;
					} else if (res.data.code == 406) {
						uni.navigateTo({
							url: res.data.page_path
						})
						resolve(res.data);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						reject();
					}
				},
				fail(e) {
					reject(e);
				}
			});
		})
	},
	get: function(url, data) {
		url = configdata.APIHOST + url;
		switch (uni.getSystemInfoSync().platform) {
			case 'android':
				url += '?medium=android' + '&version=' + version;
				break;
			case 'ios':
				url += '?medium=ios' + '&version=' + version;
				break;
			default:
				url += '?medium=mini' + '&version=' + version;
				break;
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: url,
				data: data,
				method: 'GET',
				header: {
					"Authorization": 'Bearer ' + uni.getStorageSync('USERINFO'),
				},
				success: function(res) {
					if (res.data.code == 0 || res.data.code == 3) {
						resolve(res.data);
					} else if (res.data.code == 406) {
						uni.navigateTo({
							url: res.data.page_path
						})
						resolve(res.data);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						reject();
					}
				}
			});
		})
	},
	/*
	 *   上传图片
	 * 		successFn  //回调
	 * 		num		  	//选择图片的数量
	 * 		data		//上传的参数
	 * 		progressFn	//进度的回调
	 */
	caleJiaoBiao() { //计算消息的角标
		if (!uni.getStorageSync('USERINFO')) {
			return;
		};
	},
	uploadImg(successFn, num = 1, data = {}, progressFn) { //上传图片
		let url = configdata.APIHOST;
		uni.chooseImage({
			count: num, //默认1
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //从相册选择
			success: function(res) { //选择后
				//////console.log(res, '选择的图片');
				/*uni.showLoading({
					title:'上传中...'
				})*/
				const tempFilePaths = res.tempFilePaths;
				let fileList = {}; //储存上传实例；
				for (var i in tempFilePaths) {
					fileList['uploadTask' + i] = uni.uploadFile({ //上传后台
						url: url + 'v1/5d5fa8984f0c2',
						filePath: tempFilePaths[i],
						name: 'file[]',
						formData: data,
						success: function(upRes) { //上传成功
							//uni.hideLoading();
							//////console.log(upRes, '上传图片');
							if (upRes.statusCode == 200) {
								let data = JSON.parse(upRes.data);
								if (typeof(successFn) == 'function') {
									successFn(data)
								}
							} else {
								uni.showToast({
									title: upRes.errMsg,
									icon: 'none'
								})
							}

						},
						fail: function(err) {
							//////console.log('上传失败',err);
							uni.showToast({
								title: '图片上传失败，请稍后再试.' + err.errMsg,
								icon: 'none'
							})
						}
					})
					//监听进度
					fileList['uploadTask' + i].onProgressUpdate(function(res) {
						if (typeof(progressFn) == 'function') {
							progressFn(res)
						}
						if (res.progress < 99.99) {
							uni.showLoading({
								title: res.progress + '%'
							});
						} else {
							uni.hideLoading()
						}
					});
				};
			}
		});
	}
}
